<?php
 
include_once 'autoload.php';


// $route = "/user/name/{id}";
// $regex = '~^(?:/[a-zA-Z0-9_]*|/\{([a-zA-Z0-9_]+?)\})+/?$~';
// preg_match($regex, $route, $matches);
// var_dump($matches);die;

 
use SalamanderRoute\Dispatcher;
 
$dispatcher = new Dispatcher();
 
$dispatcher->get('/', function () {
    echo 'hello world';
});
 
$dispatcher->get('/user/{id}', function ($args) {
    echo "user {$args['id']} visit";
});
 
// Fetch method and URI from somewhere
$httpMethod = $_SERVER['REQUEST_METHOD'];
$uri = $_SERVER['REQUEST_URI'];
 
// 去掉查询字符串
if (false !== $pos = strpos($uri, '?')) {
    $uri = substr($uri, 0, 
        $pos);
}

 
$routeInfo = $dispatcher->dispatch($httpMethod, $uri);

switch ($routeInfo[0]) {
    case Dispatcher::NOT_FOUND:
        echo '404 not found';
        break;
    case Dispatcher::FOUND:
        $handler = $routeInfo[1];
        $vars = $routeInfo[2];
        $handler($vars);
        break;
}